package com.aaa.controller;

import com.aaa.entity.LayData;
import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询全部用户信息
     * @return
     */
    @RequestMapping("findAllUser")
    @ResponseBody
    public LayData findAllUser(MyPage myPage,User user){
        PageInfo all = userService.findAll(myPage,user);
        LayData layData = new LayData(all);
        return layData;
    }
    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("adduser")
    @ResponseBody
    public LayData addUser(User user){
        Boolean status = userService.addUser(user);
        return new LayData(status);
    }
    /**
     * 修改用户的状态
     * @param user
     * @return
     */
    @PostMapping("updateStatus")
    @ResponseBody
    public LayData updateStatus(User user){
        Boolean isstatus=userService.updatestatus(user);
        LayData layData = new LayData(isstatus);
        return layData;
    }
    /**
     *   查询用户名是否已经使用
     */
    @PostMapping("/checkName{username}")
    @ResponseBody
    public LayData checkName(@PathVariable String username){
        Boolean isNo = userService.checkName(username);
        return new LayData(isNo);
    }

    @GetMapping("showUserByName")
    public String showUser(HttpServletRequest request, User user){
        User user1 = userService.showUserByName(user);
        request.setAttribute("user1",user1);
        return "views/showUser";
    }
    /**
     * 修改用户的密码
     * @param user
     * @return
     */
    @PostMapping("updPassword")
    @ResponseBody
    public LayData updPassword(User user){
        Boolean updpassword = userService.updpassword(user);
        LayData layData = new LayData(updpassword);
        return layData;
    }

}
